package com.andatsoft.laisim.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.util.DisplayMetrics;

public class Utils {
	public static int androidOs = Common.ANDROID_OS_GINGERBREAD;

	public static String convertTimeToString(int time) {
		int second = time / 1000;
		int minute = time / (1000 * 60);
		int hour = time / (1000 * 60 * 60);

		if (hour <= 0) {
			if (second >= 60) {

				second = second % 60;
			}
			return (minute < 10 ? "0" + minute : minute) + ":"
					+ (second < 10 ? "0" + second : second);
		} else {
			if (second >= 60) {
				if (minute >= 60) {

					minute = minute % 60;
					second = second % 60;
				}

			}
			return (hour < 10 ? "0" + hour : hour) + ":"
					+ (minute < 10 ? "0" + minute : minute) + ":"
					+ (second < 10 ? "0" + second : second);
		}
	}

	public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight,
			Context context) {
		if (photo == null || photo.isRecycled()) {
			try {
				FileInputStream f = context.openFileInput("daa");
				photo = BitmapFactory.decodeStream(f);
				f.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		float densityMultiplier = context.getResources().getDisplayMetrics().density;

		int h = (int) (newHeight * densityMultiplier);
		int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));

		photo = Bitmap.createScaledBitmap(photo, w, h, true);
		return photo;
	}

	public static Bitmap createSmallBitmap(Bitmap bm) {
		int w = bm.getWidth() / 4;
		int h = bm.getHeight() / 4;
		return Bitmap.createScaledBitmap(bm, w, h, true);
	}

	public static Bitmap boxBlur(Bitmap bmp, int range) {

		Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
				Config.ARGB_8888);
		Canvas c = new Canvas(blurred);

		int w = bmp.getWidth();
		int h = bmp.getHeight();

		int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
		bmp.getPixels(pixels, 0, w, 0, 0, w, h);

		boxBlurHorizontal(pixels, w, h, range / 2);
		boxBlurVertical(pixels, w, h, range / 2);

		c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null);

		return blurred;
	}

	private static void boxBlurHorizontal(int[] pixels, int w, int h,
			int halfRange) {
		int index = 0;
		int[] newColors = new int[w];

		for (int y = 0; y < h; y++) {
			int hits = 0;
			long r = 0;
			long g = 0;
			long b = 0;
			for (int x = -halfRange; x < w; x++) {
				int oldPixel = x - halfRange - 1;
				if (oldPixel >= 0) {
					int color = pixels[index + oldPixel];
					if (color != 0) {
						r -= Color.red(color);
						g -= Color.green(color);
						b -= Color.blue(color);
					}
					hits--;
				}

				int newPixel = x + halfRange;
				if (newPixel < w) {
					int color = pixels[index + newPixel];
					if (color != 0) {
						r += Color.red(color);
						g += Color.green(color);
						b += Color.blue(color);
					}
					hits++;
				}

				if (x >= 0) {
					newColors[x] = Color.argb(0xFF, (int) (r / hits),
							(int) (g / hits), (int) (b / hits));
				}
			}

			for (int x = 0; x < w; x++) {
				pixels[index + x] = newColors[x];
			}

			index += w;
		}
	}

	private static void boxBlurVertical(int[] pixels, int w, int h,
			int halfRange) {

		int[] newColors = new int[h];
		int oldPixelOffset = -(halfRange + 1) * w;
		int newPixelOffset = (halfRange) * w;

		for (int x = 0; x < w; x++) {
			int hits = 0;
			long r = 0;
			long g = 0;
			long b = 0;
			int index = -halfRange * w + x;
			for (int y = -halfRange; y < h; y++) {
				int oldPixel = y - halfRange - 1;
				if (oldPixel >= 0) {
					int color = pixels[index + oldPixelOffset];
					if (color != 0) {
						r -= Color.red(color);
						g -= Color.green(color);
						b -= Color.blue(color);
					}
					hits--;
				}

				int newPixel = y + halfRange;
				if (newPixel < h) {
					int color = pixels[index + newPixelOffset];
					if (color != 0) {
						r += Color.red(color);
						g += Color.green(color);
						b += Color.blue(color);
					}
					hits++;
				}

				if (y >= 0) {
					newColors[y] = Color.argb(0xFF, (int) (r / hits),
							(int) (g / hits), (int) (b / hits));
				}

				index += w;
			}

			for (int y = 0; y < h; y++) {
				pixels[y * w + x] = newColors[y];
			}
		}
	}

	@SuppressLint("SdCardPath")
	public static final String[] mCommonStoragePath = new String[] {
			"/storage/sdcard0",
			"/storage/sdcard1", // !< Motorola Xoom
			"/storage/extsdcard", // !< Samsung SGS3
			"/storage/sdcard0/external_sdcard", // user request
			"/storage/extSdcard", "/storage/extSdCard",
			"/mnt/extsdcard",
			"/mnt/sdcard/external_sd", // !< Samsung galaxy family
			"/mnt/external_sd",
			"/mnt/media_rw/sdcard1", // !< 4.4.2 on CyanogenMod S3
			"/removable/microsd", // !< Asus transformer prime
			"/mnt/emmc",
			"/storage/external_SD", // !< LG
			"/storage/ext_sd", // !< HTC One Max
			"/storage/removable/sdcard1", // !< Sony Xperia Z1
			"/data/sdext", "/data/sdext2", "/data/sdext3", "/data/sdext4",
			"/mnt/Removable/MicroSD", "/Removable/MicroSD" // zen fone
	};

	public static String[] getAllStoragePath() {
		List<String> paths = new ArrayList<String>();
		// get the ext sd card first
		paths.add(Environment.getExternalStorageDirectory().getAbsolutePath());

		// loop for the most common storage above.
		File f = null;
		for (String str : mCommonStoragePath) {
			f = new File(str);
			if (f.exists() && f.canRead() && !paths.contains(str)) {
				if (!isPathExist(paths, str))
					paths.add(str);
			}
		}
		return paths.toArray(new String[paths.size()]);
	}

	private static boolean isPathExist(List<String> src, String path) {
		String[] src1 = new File(path).list();
		String[] src2;
		for (String str : src) {
			src2 = new File(str).list();
			if (is2ArrayEquals(src1, src2))
				return true;
		}
		return false;
	}

	private static boolean is2ArrayEquals(String[] s1, String[] s2) {
		if (s1.length != s2.length)
			return false;
		for (int i = 0; i < s1.length; i++) {
			if (!s1[i].equals(s2[i]))
				return false;
		}
		return true;
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

	public static int[] convertStringToIntArray(String src) {
		if (src == null)
			return null;
		int[] vals = new int[src.length()];
		for (int i = 0; i < src.length(); i++) {
			vals[i] = src.charAt(i) - 48;

		}
		return vals;

	}

}
